package com.parking.platform.controller;


import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.Result;
import com.parking.common.core.util.JwtUtil;
import com.parking.common.web.aop.Permission;
import com.parking.common.web.util.RedisUtil;
import com.parking.platform.dto.ResourceDto;
import com.parking.platform.dto.ResourceDtos;
import com.parking.platform.form.AddResourceForm;
import com.parking.platform.form.SelResourceForm;
import com.parking.platform.form.UpResourceForm;
import com.parking.platform.form.UpResourceInfoForm;
import com.parking.platform.service.PlatformResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 香菜狗带
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/platformResource")
@Api(tags = "管理员权限接口")
public class PlatformResourceController {
    @Autowired
    private PlatformResourceService platformResourceService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 查询管理员权限菜单
     * 参数：
     * 令牌：authorization
     * 返回ResourceDto
     *
     */
    @GetMapping("/platformResourceAll.platform")
    @ApiOperation("查询管理员权限菜单")
    public Result  platformResourceAll(@RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        List<ResourceDto> list=platformResourceService.platformResourceAll(platformId);
        return Result.getListResult(ResourceDto.class).setCode(200).setMessage("查询管理员权限菜单成功").setData(list);
    }
    /**
     * 查询管理员权限菜单
     * 参数：
     *
     *  pageNum:当前页数
     *  pageSize:当前条数
     *  resourceName:资源名称
     * 返回ResourceDto
     *
     */
    @PostMapping("/showResourceAll.platform")
    @ApiOperation("展示全部权限菜单成功")
    @Permission(resources = {"/showResourceAll.platform"})
    public Result showResourceAll(@RequestBody SelResourceForm form){
        System.out.println(form+"参数");
        PageInfo page=platformResourceService.showResourceAll(form);
        System.out.println(page+"返回参数");
        return Result.getResult(PageInfo.class).setCode(200).setMessage("展示全部权限菜单成功").setData(page);
    }
    /**
     * 修改权限
     * 参数：
     * platformResourceId:平台资源id
     * platformResourceName:平台资源名称
     * platformResourceParent:平台资源父节点
     * platformResourceRouter:平台资源路由地址
     *
     * */
    @PostMapping("/upResourceData.platform")
    @ApiOperation("修改管理员权限菜单")
    @Permission(resources = {"/upResourceData.platform"})
    public Result upResourceData(@RequestBody UpResourceForm form,@RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        platformResourceService.upResourceData(form,platformId);
        return Result.getResult().setCode(200).setMessage("修改管理员权限菜单成功");
    }
    /**
     * 添加权限资源
     * 参数：
     * platformResourceName:平台资源名称
     * platformResourceParent:平台资源父节点
     * platformResourceRouter:平台资源路由地址
     *
     * */
    @PostMapping("/addResourceData.platform")
    @Permission(resources = {"/addResourceData.platform"})
    @ApiOperation("添加权限资源接口")
    public Result addResourceData(@RequestBody AddResourceForm form, @RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        platformResourceService.addResourceData(form,platformId);
        return Result.getResult().setCode(200).setMessage("添加权限资源成功");
    }
    /**
     * 查询管理员权限菜单
     * 参数：
     * 名称：platformResourceName
     * 返回ResourceDto
     * 返回前端的菜单参数对象
     * 资源Id:platformResourceId
     * 资源名称:platformResourceName
     * 资源路由名称:platformResourceRouter
     * 父节点ID：platformResourceParent
     * 二级菜单:child
     *
     */
    @GetMapping("/findResourceAll.platform")
    @ApiOperation("查询全部树状菜单")
    @Permission(resources = {"/findResourceAll.platform"})
    public Result  findResourceAll(String platformResourceName,Integer platformRoleId){
        ResourceDtos list=platformResourceService.findResourceAll(platformResourceName,platformRoleId);
        return Result.getResult(ResourceDtos.class).setCode(200).setMessage("查询全部树状菜单成功").setData(list);
    }
    /**
     * 查询全部父节点
     * 无参数：
     *
     * */
    @GetMapping("/parentResourceAll.platform")
    @ApiOperation("查询全部父节点")
    @Permission(resources = {"/parentResourceAll.platform"})
    public Result  parentResourceAll(){
        List<ResourceDto> list=platformResourceService.ParentResourceAll();
        return Result.getListResult(ResourceDto.class).setCode(200).setMessage("查询全部父节点成功").setData(list);
    }
    /**
     * 修改资源信息
     * 参数：
     *平台资源父节点：platformResourceParent
     * 平台资源id：platformResourceId
     * 平台资源父节点：platformResourceParent
     * */
    @PostMapping("/upResourceInfo.platform")
    @ApiOperation("修改资源信息")
    @Permission(resources = {"/upResourceInfo.platform"})
    public Result  upResourceInfo(@RequestBody UpResourceInfoForm form,@RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
         platformResourceService.upResourceInfo(form,platformId);
        return Result.getResult().setCode(200).setMessage("修改资源信息成功");
    }
    /**
     * 查询管理员权限菜单
     * 参数：
     * 令牌：authorization
     * 返回数组
     *
     */
    @GetMapping("/findResourceRouter")
    @ApiOperation("只展示全权限路由")
    public Result findResourceRouter(@RequestHeader("authorization")String authorization) throws Exception {
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        List<String> router=platformResourceService.findResourceRouter(platformId);
        return Result.getListResult(String.class).setCode(200).setMessage("只展示全权限路由成功").setData(router);
    }

}

